﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lenic.DI.Tests
{
    [TestClass]
    public class RegisterTest
    {
        [TestMethod]
        public void TestDefaultRegister1()
        {
            IContainer container = new Container();
            container.OnResolveError += ContainerExtensions.DefaultRegister();

            var p = container.Resolve<Person>();
            Assert.IsNotNull(p);
        }

        [TestMethod, ExpectedException(typeof(ResolveException))]
        public void TestDefaultRegister2()
        {
            IContainer container = new Container();
            container.OnResolveError += ContainerExtensions.DefaultRegister();
            container.OnResolveError -= ContainerExtensions.DefaultRegister();

            container.Resolve<Person>();
        }

        [TestMethod]
        public void TestDefaultRegister21()
        {
            IContainer container = new Container();
            container.OnResolveError += ContainerExtensions.DefaultRegister();

            var p1 = container.Resolve<Person>();
            Assert.IsNotNull(p1);

            container.OnResolveError -= ContainerExtensions.DefaultRegister();
            var p2 = container.Resolve<Person>();
            Assert.IsNotNull(p2);
        }

        [TestMethod]
        public void TestInterfaceRegister1()
        {
            IContainer container = new Container();
            var interfaceRegister = ContainerExtensions.InterfaceRegister();
            container.OnResolveError += interfaceRegister;

            var im = container.Resolve<IPerson>();
            Assert.IsNotNull(im);
            Assert.IsInstanceOfType(im, typeof(IPerson));
        }

        [TestMethod, ExpectedException(typeof(ResolveException))]
        public void TestInterfaceRegister2()
        {
            IContainer container = new Container();
            var interfaceRegister = ContainerExtensions.InterfaceRegister();
            container.OnResolveError += interfaceRegister;
            container.OnResolveError -= interfaceRegister;

            container.Resolve<IPerson>();
        }

        [TestMethod]
        public void TestInterfaceRegister21()
        {
            IContainer container = new Container();
            var interfaceRegister = ContainerExtensions.InterfaceRegister();
            container.OnResolveError += interfaceRegister;

            var p1 = container.Resolve<IPerson>();
            Assert.IsNotNull(p1);

            container.OnResolveError -= interfaceRegister;

            var p2 = container.Resolve<IPerson>();
            Assert.IsNotNull(p2);
        }

        [TestMethod]
        public void TestGenericRegister1()
        {
            IContainer container = new Container();
            container.Register(null, c => 16);
            container.OnResolveError += ContainerExtensions.GenericRegister(typeof(IList<>), typeof(List<>));

            var g = container.Resolve<IList<string>>();
            Assert.IsNotNull(g);
            Assert.IsInstanceOfType(g, typeof(IList<string>));
        }

        [TestMethod, ExpectedException(typeof(ResolveException))]
        public void TestGenericRegister2()
        {
            IContainer container = new Container();
            container.Register(null, c => 16);
            var genricRegister = ContainerExtensions.GenericRegister(typeof(IList<>), typeof(List<>));
            container.OnResolveError += genricRegister;
            container.OnResolveError -= genricRegister;

            container.Resolve<IList<string>>();
        }

        [TestMethod]
        public void TestGenericRegister21()
        {
            IContainer container = new Container();
            container.Register(null, c => 16);

            var genricRegister = ContainerExtensions.GenericRegister(typeof(IList<>), typeof(List<>));

            container.OnResolveError += genricRegister;
            var g1 = container.Resolve<IList<string>>();
            Assert.IsNotNull(g1);
            Assert.IsInstanceOfType(g1, typeof(IList<string>));

            container.OnResolveError -= genricRegister;
            var g2 = container.Resolve<IList<string>>();
            Assert.IsNotNull(g2);
            Assert.IsInstanceOfType(g2, typeof(IList<string>));
        }
    }
}